/*
 * SerialDrv.c
 *
 * Created: 10.02.2013 17:22:06
 *  Author: victor
 */

/* Serial communication peripheral driver */

/* Module's includes */
#include <Basetypes.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <SerialDrv.h>
#include <SerialHdl.h>
#include <Processing.h>
/* Module's definitions */

/* Module's function definitions */
void SerialDrv_Init ( void )
{
	/* Control and status registerB */
	UCSRB |= ( 1 << RXCIE ) | ( 1 << RXEN ) | ( 1 << TXEN ) | ( 1 << UCSZ2 ); //De aici se va citi si scrie al 9-lea bit al cadrului (primul)
	/* Control and status registerC */
	UCSRC |= ( 1 << URSEL ) | ( 1 << UCSZ1 ) | ( 1 << UCSZ0 );
	/* BaudRate register HIGH */
	UBRRH = 0;
	/* BaudRate register LOW */
	UBRRL = 143; //143 pt 4800 si respectiv 9600 bps
}

void SerialDrv_DoubleSpeed ( void )
{
	UCSRA |= ( 1 << U2X );
}

/* Interrupt for received frame, to read it's data */
ISR ( USART_RXC_vect )
{
	u16 tmp_IncomingData_u16 = SerialHdl_Getdata();
	Processing_GetIncomingMessage ( tmp_IncomingData_u16 );
}